/* <pre>
 * Copyright (c) The Loose Coupling Team.
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 *</pre>
 */

package org.lc.eic.core.pipesandfilters.impl;

import org.lc.eic.core.pipesandfilters.Filter;
import org.lc.eic.core.pipesandfilters.Pipe;
import org.lc.eic.core.pipesandfilters.exception.ProcessingException;
import org.lc.eic.core.support.logging.DefaultLogMessageDescriber;
import org.lc.eic.core.support.logging.DefaultLogger;
import org.lc.eic.core.support.logging.LogMessageDescriber;

import org.lc.eic.core.support.logging.Logger;



/**
 *  A filter implementation that only forwards to the outbound-pipe. Why such implementation? 
 *  Well it is intended to be used when you want to transparently switch underlying Pipe without the knowledge to the
 *  using logic. 
 *	@author <a href="mailto:p.lycke@googlemail.com">PaLy</a>
 *
 * 
 */
public class ForwardFilter<I> implements Filter<I, I> {
	private static final Logger logger = new DefaultLogger(ForwardFilter.class);
	private LogMessageDescriber<I> logMessageDescriber = new DefaultLogMessageDescriber<I>();
		

	private Pipe<I> outboundPipe;

	public synchronized void setOutboundPipe(Pipe<I> pipe) {
		this.outboundPipe = pipe;
		
	}

	public synchronized void process(I i) throws ProcessingException {
		if(this.outboundPipe == null){
			logger.warn("No outbound pipe is set, message will be discarded."+logMessageDescriber.logMessageDescription(i));
		} else {
			this.outboundPipe.process(i);
		}
		
	}
	
	public void setLogMessageDescriber(LogMessageDescriber<I> logMessageDescriber) {
		this.logMessageDescriber = logMessageDescriber;
	}
	
	

}
